<template>
  <container-wrapper :designer="designer" :widget="widget" :parent-widget="parentWidget" :parent-list="parentList"
                     :index-of-parent-list="indexOfParentList">
    <el-container class="card-container" @click.stop="selectWidget(widget)"
             :shadow="widget.options.shadow" :style="{width: widget.options.cardWidth + '!important' || ''}"
             :class="[selected ? 'selected' : '', !!widget.options.folded ? 'folded' : '', customClass]">
      <el-form-item label-width="0px">
        <template #default>
          <span class="suffix left">{{widget.options.preSuffix}}</span>
          <component :is="dynamicComponent" :options="widget.options.customData"></component>
        </template>
      </el-form-item>
    </el-container>
  </container-wrapper>
</template>

<script>
  import i18n from "@/utils/i18n"
  import containerMixin from "@/components/form-designer/form-widget/container-widget/containerMixin"
  import ContainerWrapper from "@/components/form-designer/form-widget/container-widget/container-wrapper"
  import refMixinDesign from "@/components/form-designer/refMixinDesign"
  import {shallowRef} from "vue";

  export default {
    name: "fy-widget",
    componentName: 'FyWidget',
    mixins: [i18n, containerMixin, refMixinDesign],
    data() {
      return {
        // 初始化为一个占位组件
        dynamicComponent: 'placeholder-component'
      };
    },
    components: {
      ContainerWrapper,
      'placeholder-component': {
        // 初始化一个占位组件的模板
        template: '<p>Loading...</p>'
      }
    },
    props: {
      widget: Object,
      parentWidget: Object,
      parentList: Array,
      indexOfParentList: Number,
      designer: Object,
    },
    computed: {
      selected() {
        return this.widget.id === this.designer.selectedId
      },

      customClass() {
        return this.widget.options.customClass || ''
      },

    },
    created() {
      let designComponentName = this.widget.options.designComponentName
      if (designComponentName){
        import(designComponentName+".vue").then((component) => {
          // 导入成功后，将组件设置为导入的组件
          this.dynamicComponent = shallowRef(component.default);
        }).catch(error => {
          // 处理导入组件时的错误
          console.error('Dynamic component loading failed:', error);
        });
      }
    },
    methods: {


    }
  }
</script>

<style lang="scss" scoped>
  .card-container.selected {
    outline: 2px solid $--color-primary !important;
  }

  .card-container {
    margin: 3px;
    :deep(.el-form-item__content) {
      flex-wrap: nowrap;
    }
  }
  .suffix{
    &.left{
      padding-right: 5px;
      white-space: nowrap;
    }
    &.right{
      padding-left: 5px;
      white-space: nowrap;
    }
  }
</style>
